package com.chzu.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chzu.demo.pojo.Teacher;
import com.chzu.demo.service.TeacherService;
import com.chzu.demo.util.MD5;
import com.chzu.demo.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师控制器")
@RestController
@RequestMapping("sms/teacherController")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @ApiOperation("分页获取教师信息，带搜索条件")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(
            @ApiParam("页码数")@PathVariable("pageNo") Integer pageNo,
            @ApiParam("分页数")@PathVariable("pageSize") Integer pageSize,
            Teacher teacher

    ){
        Page<Teacher> pageParam = new Page<>(pageNo,pageSize);
        IPage<Teacher> iPage = teacherService.getTeacherByOpr(pageParam,teacher);
        return Result.ok(iPage);
    }

    @ApiOperation("添加或修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(
            @ApiParam("要保存或修改的json格式的teacher")@RequestBody Teacher teacher){
        //如果是新增的，需要加密
        if(teacher.getId() == null || teacher.getId() == 0){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    @ApiOperation("删除单个或多个教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(
            @ApiParam("要删除的教师的json信息集合")@RequestBody List<Integer> ids
    ){
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
